package cn.bdqn.asecu.service;

import cn.bdqn.pojo.AttriSetting;
import cn.bdqn.pojo.Attributes;
import cn.bdqn.repository.Asecu_AttrManagementRepository;
import cn.bdqn.repository.Asecu_AttrSettingRepository;
import cn.bdqn.vo.AttrAndSetting;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AttributeSettingService {

    @Resource
    Asecu_AttrSettingRepository asecu_attrSettingRepository;

    @Resource
    Asecu_AttrManagementRepository asecu_attrManagementRepository;

    /**
     * 页面加载时获取数据
     * @return
     */
    public List<AttriSetting> getAttrSetting() {
        List<AttriSetting> settingList = asecu_attrSettingRepository.findAll();
        return settingList;
    }

    public List<AttrAndSetting> getAttr(Integer attrId) {
        List<AttrAndSetting> attrAndSettings = asecu_attrSettingRepository.findAttrAndSetting(attrId);
        return attrAndSettings;
    }

    public List<Attributes> getAttrManagement() {
        List<Attributes> attributes = asecu_attrManagementRepository.findByAttrId(0);
        return attributes;
    }
    @Transactional
    public boolean addSetting(Integer settingId,Integer id) {
        int i = asecu_attrManagementRepository.update(settingId, id);
        return i>0?true:false;
    }

    public AttrAndSetting findOne(Integer id) {
        AttrAndSetting as = asecu_attrSettingRepository.findAttrAndSetting1(id);
        return as;
    }

    @Transactional
    public boolean editAttrsetting(AttrAndSetting attrAndSetting) {
        int i = asecu_attrManagementRepository.edit(attrAndSetting.getAttributes().getName(), attrAndSetting.getAttributes().getId());
        int i1 = asecu_attrSettingRepository.edit(attrAndSetting.getAttrSetting().getMselect(), attrAndSetting.getAttrSetting().getId());

        if (i>0&&i1>0){
            return true;
        }
        return false;
    }
@Transactional
    public boolean delAttrsetting(Integer sid) {
        asecu_attrManagementRepository.updateAttrId(sid);

        return true;
    }
}
